<?php

//header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() - 3600));
header ('Content-type: text/html; charset=UTF-8'); 


/*
$targetFile = 'data-' . md5(microtime()) . '.csv';
$url = 'http://dados.recife.pe.gov.br/storage/f/2013-10-30T11%3A16%3A13.676Z/viatura2011.csv';
$out = fopen( dirname(__FILE__) ."/". $targetFile , 'wb');

$ch = curl_init();
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_exec($ch);


$row = 1;
$handle = fopen (dirname(__FILE__) ."/".$targetFile,"r");

$colnames=array();
$rowslines=array();
$rows=array();

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count ($data);
    //echo "<p> $num campos na linha $row: <br /></p>\n";
    $row++;
    for ($c=0; $c < $num; $c++) {
        	
		
		$cols	=	explode(";", $data[$c]);
		
		
		var_dump($cols);exit();
	
    }
	
	array_push($rowslines, (object) $colnames);
	
}
fclose ($handle);

var_dump( $rowslines);

exit();*/

?>

<script src="system.js"></script>
<script src="hmac-md5.js"></script>


<script language="javascript" type="text/javascript">

					
	function submitbutton(pressbutton) 
	{
		//var form = document.adminForm;
	
		switch( pressbutton )
		{
			
			case	"send":
			{
				
					
					
					var oObject = {} ;
					
					oObject["email"] = getInputValueElementId( "email" );
					oObject["password"] = getInputValueElementId( "password" );
					oObject["component"] = "user";
					oObject["controller"] = "user";
					oObject["task"] = "login";
					
					
					var jsonVales	=	convertToJSON(oObject);
					
					parameters	=	encodeURIComponent(
											convertToStringJSON(jsonVales));
					
					var method='GET';
						
					httpRequest ( method,
									url_API,
									parameters,
									'returnLogin');
								
						
					return false;
				
				
			}break;
			default:
			{
				
			}
		}	
		
	}


function returnLogin(jsonObj){
	
	if( jsonObj.status == 1){
		
		//alert( "data: "+jsonObj.data );
		if( getCookie("x-private") !=1 ){
			
			setCookie('x-public',jsonObj.data, 1);
			window.location.href='home_authenticated.php';
			
		}
		
		
		//
			
			
		//if(jsonObj.notification == 1)
		//
		//else
		//	alert( "data: "+jsonObj.data );
			
	}else{
		alert( jsonObj.data );
	}
	
}



function setPrivateKey(key){
	
	setCookie('x-private',key, 30);
	alert('Chave Privada salva com sucesso.');
}

function getPrivateKey_(){
	
	document.getElementById( "div-x-private").innerHTML = getCookie("x-private");
}

   	
</script>

</script>

<h1>Página inicial (não autenticado)</h1>
<div id="text-result-hidden" style="visibility:hidden;width:1px;height:1px;"></div>
<div id="text-result">Login</div>
 
<form class="cmxform" action="" method="GET">	

	<div id="label">e-mail</div> <div id="field"><input type="text" id="email" name="email"/></div>
	<div id="label">senha</div> <div id="field"><input type="password" id="password" name="password"/></div>
	
	<input type="button" name="enviar" value="Login" onclick="javascript:submitbutton('send');"/>
	
</form>

<br /><div id="label">Private Key</div> <div id="field"><input type="text" id="private_key" name="private_key"/></div>
Chave Privada Atualmente salva:<br>
<div id="div-x-private"></div>
<br>
<input type="button" name="x-private" value="Salvar Private Key em Cookie por 30 dias" onclick="javascript:setPrivateKey(getInputValueElementId('private_key'));"/>
<br>

<a href="create_user.php">Create login</a>

<script>
verity_authenticated();
getPrivateKey_();
</script>
